
def set_rf(win_size, shade, eye_cup, rf = None):
    '''
    Allow the user to use the mouse to set the position, size, and
        orientation of the receptive field object passed.
    '''
    from psychopy import event, core
    from psychopy import visual
    from psychopy.visual import Window
    import scipy
    from RF import RF
    import time
    import os

    # -------------------------------------------------
    # -- Create the window and stimuli
    # -------------------------------------------------
    myWin = Window(win_size, allowGUI=False, pos=(0,-900), fullscr=False, rgb=1.0*shade)
    
    myMouse = event.Mouse(win=myWin)
    message = visual.TextStim(myWin, pos=(0.0,0.9), alignHoriz = 'center' ,height = 0.03, 
                  text = 'left click:place center\nright click:place (0.0,1.0)\nEsc to quit.',
                  rgb = -1.0*shade )

    rf = RF.default()

    # look for helper images, with chalk in their name and ending with .jpg
    helpers = []
    files = os.listdir('.')
    for file in files:
        if 'chalk' in file and '.jpg' == file[-4:]:
            helpers.append( visual.PatchStim(myWin, tex=file, size=win_size, units='pix') )
            
        
    eye_cup.prep_for_drawing( myWin, shade=shade, opacity=1.0, force=True )

    keep_looping = True
    hi = 0
    while keep_looping: #continue until keypress

        #handle key presses each frame
        for key in event.getKeys():
            if key in ['escape','q']:
                keep_looping = False
            if key == 'up' or key == 'right':
                hi += 1
                if hi > len(helpers)-1:
                    hi -= len(helpers)
            if key == 'down' or key == 'left':
                hi -= 1
                if hi < 0:
                    hi += len(helpers)
                
        #get mouse events
        mouse_X,mouse_Y = myMouse.getPos()
        #set_X_pos =  mouse_X - win_size[0]/2.0
        #set_Y_pos = -mouse_Y - win_size[1]/2.0
        set_X_pos = mouse_X*(win_size[0]/2.0)
        set_Y_pos = mouse_Y*(win_size[1]/2.0)
        mouse1, mouse2, mouse3 = myMouse.getPressed()
        if mouse1 and not mouse2 and not mouse3:
            rf.set_center(set_X_pos, set_Y_pos)
        if mouse3 and not mouse1 and not mouse2:
            rf.set_size_ori(set_X_pos, set_Y_pos)
        
        event.clearEvents()#get rid of other, unprocessed events

        # always draw the rf last.
        rf.prep_for_drawing(myWin, shade=shade, opacity=1.0)
        eye_cup.draw()
        if len(helpers) > 0:
            helpers[hi].draw()
        rf.draw()

        # draw the onscreen instructions
        message.draw()
        
        myWin.flip()#redraw the buffer
        time.sleep(0.01)

    # save a picture of what the eye coordinates look like.
    rf.prep_for_drawing(myWin, shade=shade, opacity=1.0)
    eye_cup.prep_for_drawing(myWin, shade=shade, opacity=1.0)
    eye_cup.draw()
    rf.draw()
    myWin.flip()
    eye_cup.draw()
    rf.draw()
    core.wait(0.5)
    myWin.getMovieFrame()
    myWin.saveMovieFrames('rf.jpg')

    myWin.close()
    return rf


if __name__ == '__main__':
    from Eye_cup import Eye_cup

    win_size = (1440,900)
    shade = 1.0  # 1.0 for white, -1.0 for black
    eye_cup = Eye_cup.from_eye_cup_file()

    rf = set_rf(win_size, shade, eye_cup)
    rf.write_rf_file()
